<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UbahPassword extends zulController {
	function __construct(){
		parent::__construct();
		$this->load->model("UserModel");
		$this->checkLogin();
	}
	public function index(){
		$this->load->library("form_validation");
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'pwd','label'=>'Password Lama','rules'=>'required|callback_check_pwd'),
		array('field'=>'pwd_new','label'=>'Password Baru','rules'=>'required'),
		array('field'=>'pwd_kon','label'=>'Konfirmasi Password Baru','rules'=>'required|matches[pwd_new]'),
		));
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in"><button type="button" class="close" data-dismiss="alert">&times;</button>', '</div>');
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		$this->form_validation->set_message('matches','Kolom %s tidak sama !');
		if($this->form_validation->run() == FALSE){
			$data["active"] = "user_menu";
			$data["content"]= $this->load->view("formUbahPassword",$data,true);
			$this->load->view("main_new",$data);
		} else {
			$id	= $this->session->userdata("id_user");
			$pwd= md5($this->input->post("pwd_new"));
			$this->UserModel->save(array("PASSWORD"=>$pwd),$id);
			redirect("/Home");
		}
	}
	function check_pwd($pwd){
		$id	= $this->session->userdata("id_user");
		$getUser	= $this->UserModel->get($id);
		if($getUser["PASSWORD"] == md5($pwd)){
			return TRUE;
		} else {
			$this->form_validation->set_message("check_pwd","Password lama tidak cocok");
			return FALSE;
		}
	}
}